#include "mainscene.h"
#include "ui_mainscene.h"
#include<QPainter>
#include"mypushbutton.h"
#include<QDebug>
#include<QTimer>
#include<QMediaPlayer>
#include<QSoundEffect>
mainScene::mainScene(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::mainScene)
{
    ui->setupUi(this);

    //主窗口编辑
    setFixedSize(320,588);
    setWindowIcon(QIcon(":/res/Coin0001.png"));
    //窗口标题
    setWindowTitle("翻硬币主界面");
    //退出按钮的实现
    connect(ui->actionquit,&QAction::triggered,[=](){
        this->close();

    });
    //退出按钮
    myPushButton *quitbtn = new myPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    quitbtn->setParent(this);
    quitbtn->move(this->width() - quitbtn->width(),this->height() - quitbtn->height());
    connect(quitbtn,&QPushButton::clicked,this,[=](){
        QTimer::singleShot(300,this,[=](){
            //发送信号给主场景
            this->close();
        });
    });
    //准备开始按钮的音效
//    QMediaPlayer *startSound = new QMediaPlayer;
//    startSound->setSource(QUrl("TapButtonSound.wav"));




    //开始按钮
    myPushButton * strbtn =new myPushButton(":/res/MenuSceneStartButton.png");
    strbtn->setParent(this);
    strbtn->move(this->width()*0.5 - strbtn->width()*0.5,this->height()*0.65);

    //创建选关界面
    choose_scene = new chooseScene;
    //监听选择关卡的返回信号
    connect(choose_scene,&chooseScene::ChooseSceneBack,this,[=](){
        //设置this出现位置
        this->setGeometry(choose_scene->geometry());
        //选关界面隐藏
        this->show();
        //自身出现
        choose_scene->hide();

    });


    connect(strbtn,&QPushButton::clicked,this,[=](){
        //弹起特效
        strbtn->zoomdown();
        strbtn->zoomup();
        QSoundEffect *startSound = new QSoundEffect;
        startSound->setSource(QUrl::fromLocalFile("TapButtonSound.wav"));
        startSound->setVolume(1.0f);
        startSound->setLoopCount(1);  //循环次数
        startSound->setParent(this);
        startSound->play();

        //延时进入
        QTimer::singleShot(500,this,[=](){
            //设置choose_scene出现位置
            choose_scene->setGeometry(this->geometry());
            //自身隐藏
            this->hide();
            //选关界面出现
            choose_scene->show();




        });





    });





}
//背景图
void mainScene::paintEvent(QPaintEvent *){
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/bg.jpg");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //画背景的图标
    pix.load(":/res/Title.png");
    pix=pix.scaled(pix.width()*0.5,pix.height()*0.5);
    painter.drawPixmap(10,30,pix);


}



mainScene::~mainScene()
{
    delete ui;
}

